package com.galengao.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @author GalenGao
 * @version Id: Test.java, v 0.1 2022/9/15 17:48 GalenGao Exp $$
 */
@Data
@Builder
public class JsonOutput implements Serializable {

    private String projectName;
    private String projectMangeUnit;
    private String executingUnit;
    private String executingUnitCode;
    private BigDecimal investmentAmount;
    private BigDecimal annualProjectBudget;
    private Integer annualBudgetYear;
    @JsonFormat(pattern = "YYYY-MM-DD HH:mm:ss")
    private Date projectStartTime;

    @JsonFormat(pattern = "YYYY-MM-DD HH:mm:ss")
    private Date projectEndTime;

    private String projectConstructioAndScale;

    private byte[] projectManagerLetter;
    private byte[] projectInvestmentDetail;
    private byte[] partyOrOfficeSummary;

    private Integer applicationType;
    private Integer constructLevel;
    private Boolean isUnified;
    private String applicationField;
    private String bussinessField;
    private String userScope;
    private String netEnvironment;
    private String projectCode;

    private List<FileEntity> approvalDocuments;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class FileEntity {
        private String name;
        private byte[] content;
        private String type;
    }

    public static void main(String[] args) throws Exception {

        String url = "https://img0.baidu.com/it/u=4050062314,1116360005&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500";
        byte[] file = getImageFromNetByUrl(url);
        List<FileEntity> fileEntities =
                Arrays.asList(new FileEntity("审批文件", file,"批复类型1"), new FileEntity("专家评审", file,"批复类型2"));
        ObjectMapper objectMapper = new ObjectMapper();
        JsonOutput test = JsonOutput.builder().projectName("数字资源管理项目").projectMangeUnit("主管单位").executingUnit("实施单位")
                .executingUnitCode("1212121212").investmentAmount(new BigDecimal("10")).
                        annualProjectBudget(new BigDecimal("100")).annualBudgetYear(2022)
                .projectStartTime(Date.from(LocalDateTime.now().plusDays(566).atZone(ZoneId.systemDefault()).toInstant()))
                .projectEndTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()))
                .projectConstructioAndScale("项目建设内容及规模").projectManagerLetter(file)
                .projectInvestmentDetail(file).partyOrOfficeSummary(file).
                        applicationType(1).constructLevel(1).isUnified(true)
                .applicationField("1").bussinessField("1").userScope("用户范围")
                .netEnvironment("网络环境").projectCode("FSN202209150001")
                .approvalDocuments(fileEntities)
                .build();
        String s = objectMapper.writeValueAsString(test);
        System.out.println(s);
    }


    public static byte[] getImageFromNetByUrl(String strUrl) {
        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();
            byte[] btImg = readInputStream(inStream);
            return btImg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从输入流中获取数据
     *
     * @param inStream 输入流
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[10240];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }

}
